package me.zhengjie.server.impl;

import lombok.AllArgsConstructor;
import me.zhengjie.dao.MaterialDetailDao;
import me.zhengjie.server.MaterialService;
import me.zhengjie.vo.MaterialVo;
import me.zhengjie.server.MaterialVoService;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@AllArgsConstructor
public class MaterialVoServiceImpl implements MaterialVoService {


    @Override
    public List<MaterialVo> add(List<MaterialDetailDao> detailList) {
        // 使用Map按materialCode分组，合并相同code的细节
        Map<String, MaterialVo> materialMap = new HashMap<>();
        for (MaterialDetailDao detail : detailList) {
            String code = detail.getMaterialCode();
            // 获取或创建MaterialVo（自动合并相同code的记录）
            MaterialVo vo = materialMap.computeIfAbsent(code, k -> {
                MaterialVo newVo = new MaterialVo();
                newVo.setMaterialCode(k);
                newVo.setTotalWeight(0.00); // 初始化重量
                newVo.setNeedWeight(0.00);
                return newVo;
            });

            // 合并重量（累加）
//            vo.setTotalWeight(detail.getWeight());
//            vo.setNeedWeight(vo.getNeedWeight() + detail.getWeight());
        }

        // 返回合并后的列表
        return new ArrayList<MaterialVo>(materialMap.values());
    }
}
